package es.udc.fbellas.j2ee.tiendaweb.model.usuario.DAO;

import es.udc.fbellas.j2ee.util.configuration.ConfigurationParametersManager;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;


// For testing.
import java.sql.Connection;
import javax.sql.DataSource;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;    
import es.udc.fbellas.j2ee.tiendaweb.model.usuario.TO.UsuarioProfileTO;

/**
 * A factory to get <code>SQLUsuarioProfileDAO</code> objects.
 * <p>
 * Required configuration parameters:
 * <ul>
 * <li><code>SQLUsuarioProfileDAOFactory/daoClassName</code>: it must specify the 
 * full class name of the class implementing <code>SQLUsuarioProfileDAO</code>.
 * </li>
 * </ul>
 */
public final class SQLUsuarioProfileDAOFactory {

    private final static String DAO_CLASS_NAME_PARAMETER =
        "SQLUsuarioProfileDAOFactory/daoClassName";

    private final static Class daoClass = getDAOClass();
    
    private SQLUsuarioProfileDAOFactory() {}
    
    private static Class getDAOClass() {
    
        Class theClass = null;
    
        try {
        
            String daoClassName = 
                ConfigurationParametersManager.getParameter(
                    DAO_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(daoClassName);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return theClass;
        
    }
    
    public static SQLUsuarioProfileDAO getDAO() throws InternalErrorException {
        
        try {        
            return (SQLUsuarioProfileDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
        
    }
    
    /* 
     * Test code. Uncomment for testing. Test this class and any class
     * implementing "SQLUserProfileDAO".
     */         
    public static void main (String[] args) {    
        
        Connection connection = null;
    
        try {        
            
            /* Get a connection. */
            DataSource dataSource = new SimpleDataSource();
            connection = dataSource.getConnection();        
                    
            /* Get dao. */
            SQLUsuarioProfileDAO dao = SQLUsuarioProfileDAOFactory.getDAO();

            /* Test "SQLUsuarioProfileDAO::create". */
            System.out.println("Test for 'SQLUsuarioProfileDAO::create'");
            UsuarioProfileTO usuarioProfileTO = new UsuarioProfileTO("fbellas",
                "YYYYYYY", "Fernando", "Bellas Permuy",
                    "fbellas@udc.es", 12345, 12345, "es", "es");
            dao.create(connection, usuarioProfileTO);
            System.out.println("usuarioProfileTO created => " + usuarioProfileTO);
            
            /* Test "SQLUsuarioProfileDAO::exists". */
            System.out.println("Test for 'SQLUsuarioProfileDAO::exists'");
            if (!dao.exists(connection, usuarioProfileTO.getLogin())) {
                throw new Exception("No se puede encontrar usuario profile con " +
                    "login = " + 
                    usuarioProfileTO.getLogin());
            }
            
            /* Test "SQLUsuarioProfileDAO::find". */
            System.out.println("Test for 'SQLUsuarioProfileDAO::find'");
            System.out.println(dao.find(connection,
                usuarioProfileTO.getLogin()));
            
            /* Test "SQLUsuarioProfileDAO::update". */
            System.out.println("Test for 'SQLUsuarioProfileDAO::update'");
            usuarioProfileTO.setNombre("John");
            usuarioProfileTO.setApellidos("Smith");
            usuarioProfileTO.setEmail("jsmith@acme.com");
            usuarioProfileTO.setEncryptedPassword("ZZZZZZZZ");
            dao.update(connection, usuarioProfileTO);
            System.out.println(dao.find(connection,
                usuarioProfileTO.getLogin()));
                
            /* Tests OK. */
            System.out.println("Tests OK !!!!");
        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                GeneralOperations.closeConnection(connection);
			} catch (InternalErrorException k){
				k.printStackTrace();
			}
        }
                
    }    
    
}
